package com.mycompany.webapp.action;

import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.output.ByteArrayOutputStream;
import org.richfaces.component.UIEditor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.mycompany.core.ITemplateManager;

@Component("template")
@Scope("request")
public class TemplateBean {

	public void readOnly(){
		this.editor.setReadonly(true);
	}
	
	public void fullAccess(){
		this.editor.setReadonly(false);
	}
	
	public void save() { 
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		Map<String, Object> params = new HashMap<String, Object>();
		List<String> contents = new ArrayList<String>();
		contents.add(headerTemplate);
		contents.add(content);
		contents.add(footerTemplate);
		
		params.put("date", date);
		params.put("address", address);
		params.put("email", email);
		params.put("phone", phone);

		templateManager.convertToPdf(contents,params, bos);
		sendFile(bos);
	}

	/**
	 * Sends file to client
	 * @param bos OutputStream with pdf data
	 */
	private void sendFile(ByteArrayOutputStream bos){
		try {
			FacesContext context = FacesContext.getCurrentInstance();
			HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
			int read = 0;
			byte[] bytes = new byte[1024];
			response.setContentType("application/pdf");
			response.setHeader("Content-Disposition", "attachment;filename=\"" + "template.pdf" + "\"");
			InputStream fis = null;
			OutputStream os = null;
			fis = new ByteArrayInputStream(bos.toByteArray());
			os = response.getOutputStream();
			while ((read = fis.read(bytes)) != -1) {
				os.write(bytes, 0, read);
			}
			os.flush();
			os.close();
			bos.close();
			FacesContext.getCurrentInstance().responseComplete();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public Boolean getReadOnly() {
		return readOnly;
	}

	public void setReadOnly(Boolean readOnly) {
		this.readOnly = readOnly;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPhone() {
		return phone;
	}
	
	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}
	public UIEditor getEditor() {
		return editor;
	}

	public void setEditor(UIEditor editor) {
		this.editor = editor;
	}
	public ITemplateManager getTemplateManager() {
		return templateManager;
	}
	
	public void setTemplateManager(ITemplateManager templateManager) {
		this.templateManager = templateManager;
	}

	@Autowired
	private ITemplateManager templateManager;
	private UIEditor editor;
	private String name;
	private String headerTemplate = "<h1 style='float: left; margin: 0; padding: 0;'><img width='100' src='http://localhost:8080/web/images/tele2-logo.jpg'/></h1><div style='float: right; font-size: small;'>Tele2 Kundenservice<br/> Tel: ${phone}<br/>${email}<br/>${address}</div><div style='clear: both;'>&nbsp;</div><br/><br/><br/><br/>Anrede verliehene Titel<br/>Akad. Grad Vorname Nachname<br/>Strasse<br/>Ort";
	private String footerTemplate = "<br/><p>Mit freundlichen Grussen.</p><img src='http://localhost:8080/web/images/tele2-sign.jpg'/><p>Milosch Godina</p><p>Leiter Kundenservice</p><br/><br/><br/><br/><p style=\"text-align: right;  font-size: x-small;\">Tele2 Telecommunication GmbH / Donau-City-Strasse 11/1220 Wien<br/>Firmenbuchnummer FN 138197g / Firmenbuchgericht: HG Wien<br/>Firmensitz Wien / UID-Nr. ATU 39553103 / DVR-Nr.: 0871290</p>";
	private String content = "<p><strong>Ihre Kundennummer: $kundennummer<br /></strong></p><p style=\"text-align: right;\">$date</p><p>Seher geherter Herr Mag. Benedict,</p><p>ich entschuldige mich fur die entstandenen Unannehmlichkeiten.</p><p>Ich habe alle offenen Verbindungsentgelte in Hohe von EUR $EUR ausgebucht und Ihre Rufnummer $rufnummer in unserem System deaktiviert.</p><p>Ich bedauere die Umstande sehr und wurde mich freuen, wenn wir Sie in Zukunft doch noch als Tele 2 Kunde&nbsp; begrussen durfen.</p><p>Haben Sie noch Fragen ? Fur Ruckfragen stehe ich Ihnen gerne unter der Rufnummer 01/9009-$tele2rufnummer zur Verfugung.</p>";
	private String address;
	private String email;
	private String phone;
	private String date;
	private Boolean readOnly;
}
